<?php
namespace app\backstage\validate;
use think\Validate;
class ManagerValidate extends Validate
{
    protected $rule = [
        'id'        => 'require|gt:0',
        'role_id'   => 'require|gt:0',
        'title'     => 'require',
        'nickname'  => 'require',
        'status'    => 'require',
        'account'   => 'require',
        'password'  => 'require|length:4,25',
        'vercode'       => 'require|length:4|captcha',
        'oldPassword'   => 'require',
        'repassword'    => 'require|confirm:password',
        'remarks'       => 'max:100',
        'quick_ids'     => 'checkQuickIds',
        'sharing_ratio' => 'require|egt:0',
        'allow_cache'   => 'in:0,1'
    ];

    protected $message  =   [
        'id.require'        => '账号id不能为空',
        'id.gt'             => '账号id不能小于0',
        'title.require'     => '请填写账号名称',
        'nickname.require'  => '请填写账号昵称',
        'status.require'    => '请选择账号状态',
        'role_id.require'   => '请选择账号所属角色',
        'role_id.gt'        => '请选择账号所属角色',
        'vercode.length'    => '验证码长度错误',
        'vercode.captcha'   => '验证码错误',
        'account.max'       => '账号不允许超过20个字符',
        'password.require'  => '密码不能为空',
        'sharing_ratio.require'  => '分成比例不能为空',
        'sharing_ratio.egt'  => '分成比例不能小于0',
    ];

    protected $scene = [
        'del'   => ['id'],
        'edit'  => ['id','role_id','account','nickname','status'],
        'add'   => ['role_id','account','nickname','status','allow_cache'],
        'login' => ['password', 'password', 'vercode'],
        'password' => ['oldPassword', 'password', 'repassword'],
        'editRemarks' => ['id', 'remarks'],
        'managerEmpowerApp' => ['id', 'quick_ids'],
        'editUser' => ['id', 'remarks','allow_cache'],
        'reset_pwd' => ['id','password'],
        'editsharing_ratio' => ['id', 'sharing_ratio'],
    ];

    // 自定义验证规则
    protected function checkQuickIds($value,$rule,$data=[])
    {
        if (!preg_match('#^[1-9][0-9]*(,[1-9][0-9]*)*$#', $value)) {
            return '授权Appid格式异常';
        }
        return true;
    }


    public function sceneAddUser()
    {
        return $this->only(['account', 'role_id', 'remarks','status'])
            ->append('account', 'max:20')
            ->append('status', 'in:0,1')
            ->remove('status', 'require');
    }

    public function sceneEditUser()
    {
        return $this->only(['id', 'status', 'remarks'])
            ->append('status', 'in:0,1')
            ->remove('status', 'require');
    }
}